<?php

namespace app\admin\controller;

use app\admin\controller\Admin;
use think\Db;

/**
 * @title 订单
 */
class Order extends Admin {

    /**
     * @title 文章框架
     */
    public function iframe() {
        // 加载内容
        $this->assign('iframe_src', url('admin/order/index'));
        // 加载分类
        $this->assign('nodes', model('order')->iframe_category_config_json('order_status'));
        return $this->fetch('base:iframe');
    }

    /**
     * @title 列表
     */
    public function index() {


        $status = input('get.category', NULL);

        session('redirect', url('index') . '?' . http_build_query(request()->get()));

        $wheres = $status ? ['status' => ['=', $status]] : [];

        $lists = model('order')->model_where($wheres)->order('id desc')->paginate(config('base.page_size'), false, ['query' => request()->get()]);

        $this->assign('lists', $lists);
        $this->assign('page', $lists->render());

        builder('list')
                ->addItem('id', '#')
                ->addItem('nickname', '微信名')
                ->addItem('order_number', '订单号')
                ->addItem('shipping_name', '收件人')
                ->addItem('total', '总价')
                ->addItem('express_name', '快递')
                ->addItem('express_number', '快递单号')
                ->addItem('ip', '下单IP')
                ->addItem('create_time', '下单时间')
                ->addItem('status_text', '订单状态')
                ->addAction('处理', 'view', '<i class="layui-icon layui-icon-search"></i>', 'openbox_big')
                ->build();

        return view();
    }

    /**
     * @title 删除订单
     */
    public function deletes() {
        //
        $ids = input('post.ids/a');
        empty($ids) && $this->error('没有选择任何数据');

        $affect_rows = db('order')->where('id', 'in', $ids)->update(['delete_time' => time()]);
        if ($affect_rows) {
            $this->success('', url('index', ['category' => session('category')]));
        } else {
            $this->error('删除失败');
        }
    }
   
    /**
     * @title 查看订单的详情
     * @param type $id
     */
    public function view($id) {         

        if ($id) {

            $order_one = model('order')->where('id', $id)->find()->toArray();
            
            //收货人            
            $order_one['receiver_address'] = $order_one['shipping_province'] . $order_one['shipping_city'] . $order_one['shipping_county'] . ' '
                    . $order_one['shipping_address_detail'];
            $order_one['receiver'] = $order_one['shipping_name'] . ' ' . $order_one['shipping_tel'];            
            $this->assign($order_one);
            
            //订单相关联的商品
            $this->assign('goods_lists', db('order_goods')->where('order_id', $id)->select());
            
            //订单的跟踪记录
            $this->assign('order_history_lists', $order_history_lists = model('order_history')->where('order_id', $id)->select());

            //加载快递公司下拉
            $this->assign('express_select', db('express')->column('id,title'));
            
            // 订单状态
            $this->assign('status_select', config('order_status'));
            
            //
            return view();
        }
    }

    /**
     * @title 订单状态删除
     * 
     */
    public function order_history_delete() {
        $id = input('get.id', 0);
        if ($id) {
            $affect_rows = db('order_history')->where('id', $id)->delete();
            if ($affect_rows) {
                $this->success('');
            } else {
                $this->error('删除失败');
            }
        }
    }

    /**
     * @title 订单状态变更
     * @param type $id
     */
    public function order_history_update($id = 0) {
        $id = intval(input("request.id"));
        empty($id) && $this->error('参数不能为空');
        // $post = input('post.')
        $status = input('post.status');
        $old_status = input('post.old_status');
        if ($status < $old_status) {
            $this->error('状态设置有误');
        }
        $remark_config = config('order_status');
        $remark = input('post.remark', '');
        if (empty($remark)) {
            if (isset($remark_config[$status])) {
                $remark = $remark_config[$status];
            }
        }
        // print_r($remark);exit;
        if (request()->isPost()) {
            //insert($order_id, $status, $comment, $notify = 0)
            // $id, $status, input('post.remark'), input('post.notify')
            $insert_id = db('order_history')->insert([
                'order_id' => $id,
                'order_status' => $status,
                'remark' => $remark,
                'create_time' => time(),
                'notify' => input('post.notify', 0)
            ]);
            if (is_numeric($insert_id) && $insert_id > 0) {
                //是否发邮件通知 必须在留有电子邮箱的情况下
                if (input('post.notify')) {
                    //TODO
                }
                //更新订单的状态
                //如果状态是已发货，把快递单号也更新了
                $post['status'] = $status;
                $post['express_id'] = input('post.express_id');
                $post['express_number'] = input('post.express_number');

                db('order')->where('id', $id)->update($post);

                $this->success('');
            } else {
                $this->error($insert_id);
            }
        }
    }

    /**
     * @title 添加
     */
    public function add() {
        if (request()->isPost()) {
            $object = model('order')->insert();
            if (is_numeric($object)) {
                $object > 0 ? $this->success('订单') : $this->error('订单添加失败');
            } else {
                $this->error($object);
            }
        } else {
            $this->assign_form(array(
                'pid' => ['select', '产品', ''],
                'count' => ['input', '数量', ''],
                'name' => ['input', '姓名', ''],
                'tel' => ['input', '电话', ''],
                'citys' => ['citys', '省市区县', ''],
                'address' => ['input', '地址', ''],
                'remark' => ['textarea', '备注', ''],
                'email' => ['input', '邮箱', ''],
                'payment_code' => ['select', '支付状态', ''],
                'status' => ['radio', '发货状态', ''],
                'express_id' => ['select', '快递公司', ''],
                'express_number' => ['input', '快递单号', ''],
            ));
            return view();
        }
    }

 

    /**
     * @title 售后处理框架
     */
    public function iframe_service() {

        $parent = [
            'id' => 0,
            'name' => '所有分类',
            'spread' => true,
            'children' => [
                    ['id' => 0, 'name' => '待处理'],
                    ['id' => 1, 'name' => '处理中'],
                    ['id' => 10, 'name' => '处理完成'],
                    ['id' => -1, 'name' => '用户撤消'],
            ]
        ];

        // 加载内容
        $this->assign('iframe_src', url('admin/order/service'));
        // 加载分类
        $this->assign('nodes', json_encode($parent));
        return $this->fetch('base:iframe');
    }

    /**
     * @title 售后处理
     * 
     */
    public function service() {

        //
        $status = input('get.category', NULL);

        session('redirect', url('index') . '?' . http_build_query(request()->get()));

        $wheres = [
            'status' => ['=', $status]
        ];

        $lists = model('order_service')->model_where($wheres)->order('id desc')->paginate(config('base.page_size'), false, ['query' => request()->get()]);

        $this->assign('lists', $lists);
        $this->assign('page', $lists->render());

        builder('list')
                ->addItem('id', '#')
                ->addItem('nickname', '会员')
                ->addItem('goods_image', '商品图片', 'image')
                ->addItem('goods_title', '商品名称')
                ->addItem('goods_price', '价格')
                ->addItem('type_text', '售后类型')
                ->addItem('message', '问题描述')
                ->addItem('create_time', '下单时间')
                ->addItem('status_text', '订单状态')
                ->build();

        return view();
    }

    /**
     * @title 售后状态变更 
     */
    public function service_status_change($ids = [], $value = 0) {

        empty($ids) && $this->error('售后不能为空');

        $affect_rows = db('order_service')->where('id', 'in', $ids)->setField('status', $value);
        is_numeric($affect_rows) ? $this->success('') : $this->success('没有更新任何数据');
    }

    /**
     * @title 删除售后
     */
    public function service_delete($ids = []) {


        empty($ids) && $this->error('参数不能为空');

        $affect_row = db('order_service')->where('id', 'in', $ids)->delete();
        if ($affect_row) {
            $this->success('');
        } else {
            $this->error('删除失败');
        }
    }

}
